package com.mind.xgame.tools.config;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

import freemarker.template.Configuration;
import freemarker.template.ObjectWrapper;
import freemarker.template.TemplateExceptionHandler;

public class ToolsConf {
	public final static String CONFIG_FILE="/tools.conf";
	public final static String MESSAGE_FILE="../mind-test/resources/message/protocol/message.xml";
	public final static String JAVA_OUT_PATH="../mind-test/src/main/java/";
	public final static String JAVA_OUT_PATH_MESSAGE="../mind-test/src/main/java/MessageCode.java";
	public final static String CPP_OUT_PATH="D:/work/back/cocos2d-x-2.2.2/projects/xyz/Classes/";
	public final static String CPP_OUT_PATH_MESSAGE="D:/work/back/cocos2d-x-2.2.2/projects/xyz/Classes/MessageCode.h";
	private final static String ftl_dir="/ftl";
	private Configuration cfg;
	private static ToolsConf conf=null;
	private Properties properties=new Properties();
	public static String curPath=null;
	private List<DataBigType> bigTypes=new ArrayList<DataBigType>();
	private List<DataBaseType> baseTypes=new ArrayList<DataBaseType>();
	public static ToolsConf getInstance(){
		if(conf==null){
			conf=new ToolsConf();
			conf.initConf();
		}
		return conf;
	}
	private void initFtl() {
		// 初始化FreeMarker配置;
		// - 创建一个配置实例
		cfg = new Configuration();
		// - 设置模板目录.
		cfg.setClassForTemplateLoading(this.getClass(), ftl_dir);
		// - 设置模板延迟时间，测试环境设置为0，正是环境可提高数值.
		cfg.setTemplateUpdateDelay(0);
		// - 设置错误句柄
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
		cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
		// - 设置默认模板编码
		cfg.setDefaultEncoding("utf-8");
		// - 设置输出编码
		cfg.setOutputEncoding("utf-8");
		cfg.setLocale(Locale.SIMPLIFIED_CHINESE);
	}
	public  void initConf(){
		try {
			URL url=	this.getClass().getResource(CONFIG_FILE);
			curPath=url.getPath().substring(0, url.getPath().indexOf(CONFIG_FILE));
			properties.load(new FileInputStream(url.getPath()));
			initFtl();
			initDataBigTypes();
			initDataBaseType();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Configuration getCfg() {
		return cfg;
	}
	private void initDataBigTypes(){
		String value= properties.getProperty("DataBigType");
		String[] types= value.split("><");
		for(String type:types){
			String[] objs=type.split(",");
			DataBigType bigType=new DataBigType(objs[0],Integer.parseInt(objs[1]));
			bigTypes.add(bigType);
		}
	}
	public List<DataBigType> getDataBigTypes(){
		return bigTypes;
	}
	private void initDataBaseType(){
		String value= properties.getProperty("DataBaseType");
		String[] types= value.split("><");
		for(String type:types){
			String[] objs=type.split(",");
			int bigType=Integer.parseInt(objs[3]);
			
			DataBaseType baseType=new DataBaseType(objs[0],objs[1],objs[2],getBigTypeByValue(bigType));
			baseTypes.add(baseType);
		}
	}
	public DataBigType getBigTypeByValue(int value){
		for(DataBigType bigType:bigTypes){
			if(bigType.getValue()==value){
				return bigType;
			}
		}
		return null;
	}
	public List<DataBaseType> getDataBaseType(){
		return baseTypes;
	}
	public String[] getServerDataType(){
		String value= properties.getProperty("data_type");
		return value.split(",");
	}
	public String[] getClientDataType(){
		String value= properties.getProperty("data_type");
		return value.split(",");
	}
	public String getCurPath() {
		return curPath;
	}


}
